package net.wanho.controller;

import net.wanho.po.Student;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * Author：汤小洋
 * Date：2024-03-27 11:30
 * Description：<描述>
 */
@RestController
public class ProducerController {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Value("${shop.exchange}")
    private String exchange;

    @Value("${shop.routingKey}")
    private String routingKey;

    @GetMapping("/send")
    public String send(){
        // rabbitTemplate.convertAndSend("exchange1.direct", "key1", "hello");

        // rabbitTemplate.convertAndSend("exchange1.direct", "key1", new Student(1001, "张三"));

        // rabbitTemplate.convertAndSend(exchange, routingKey, new Student(1002, "李四"));

        rabbitTemplate.convertAndSend("exchange2.fanout", "", new Student(1003, "王五"));

        return "success";
    }

    @GetMapping("/send2")
    public String send2(){
        // 为消息指定一个唯一的id
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend("exchange3.direct", "hello", "111", correlationData);
        return "success";
    }

    @GetMapping("send3")
    public String send3(){
        for(int i=1;i<300;i++) {
            CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
            rabbitTemplate.convertAndSend("normal.exchange", "normal.key", "消息" + i, correlationData);
        }

        return "success";
    }

}
